package juc.atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * @author fangbin
 * @desc 演示LongAccumulator用法
 * @date 2020/6/15 22:05
 */
public class LongAccumulatorDemo {

    public static void main(String[] args) {
        LongAccumulator longAccumulator = new LongAccumulator((x,y)->x+y,0);
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        IntStream.range(1,10).forEach(i->executorService.submit(()->longAccumulator.accumulate(i)));
        executorService.shutdown();
        while (!executorService.isTerminated()){

        }
        System.out.println(longAccumulator.getThenReset());
    }
}
